﻿using MySchool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RecordManager
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 清空数据
            clearAll();
        }

        private void clearAll()
        {
            this.GoodsName.Text = "";
            this.DrawNum.Text = "";
            this.DrawDate.Text = "";
            this.DrawReason.Text = "";
            this.DrawMan.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //实现新增功能
            //判断
            bool flag = check();
            if (flag)
            {
                //调用新增方法
                addSave();
            }
            else {
                MessageBox.Show("所有文本框不能为空，请确认填写！");
            }
        }

        private bool check()
        {
            //拿到窗体上的数据
            string GoodsName = this.GoodsName.Text.Trim();
            string DrawNum = this.DrawNum.Text.Trim();
            string DrawDate = this.DrawDate.Text.Trim();
            string DrawReason = this.DrawReason.Text.Trim();
            string DrawMan = this.DrawMan.Text.Trim();
            if (!GoodsName.Equals(string.Empty) &&
                !DrawNum.Equals(string.Empty) &&
                !DrawDate.Equals(string.Empty) &&
                !DrawReason.Equals(string.Empty) &&
                !DrawMan.Equals(string.Empty)) {
                return true;
            }
            return false;
        }

        private void addSave()
        {
            //拿到窗体上的数据
            string GoodsName = this.GoodsName.Text.Trim();
            int DrawNum = Convert.ToInt32(this.DrawNum.Text.Trim());
            string DrawDate = this.DrawDate.Text.Trim();
            string DrawReason = this.DrawReason.Text.Trim();
            string DrawMan = this.DrawMan.Text.Trim();
            DBHelper dBHelper = new DBHelper();
            try
            {
                dBHelper.OpenConnection();
                // 定义sql语句
                string sql = "INSERT [dbo].[Record] ([GoodsName], [DrawNum], [DrawDate], [DrawReason], [DrawMan]) VALUES " +
                    "(N'" + GoodsName + "', "+ DrawNum +", N'" + DrawDate + "', N'" + DrawReason + "', N'" + DrawMan + "')";
                SqlCommand command = new SqlCommand(sql, dBHelper.Connection);
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("提交成功！");
                    clearAll();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally {
                dBHelper.CloseConnection();
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
